<?php
namespace addons\workteam;

use app\common\library\Menu;
use think\Addons;
use think\Db;
use think\Log;
use think\Queue;

/**
 * 插件
 */
class Workteam extends Addons
{
    private  $addon_name="workteam";
    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        $menu=[];
        $config_file= ADDON_PATH .$this->addon_name . DS.'config'.DS. "menu.php";
        if (is_file($config_file)) {
            $menu = include $config_file;
        }
        if($menu){
            Menu::create($menu);
        }
        //首次安装创建表并导入默认数据
        \think\addons\Service::importsql($this->addon_name);
        $this->importData('defaultdata');

        /**
         * 增加默认方法
         */
        $database=config('database');
        $isexits=Db::query("SELECT 1 FROM  information_schema.COLUMNS WHERE  table_name='{$database['prefix']}admin' AND COLUMN_NAME='mobile' limit 1");
        if (!$isexits){
            Db::query("ALTER TABLE {$database['prefix']}admin ADD mobile VARCHAR(20)  NULL COMMENT '手机'");
        }

        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        $info=get_addon_info($this->addon_name);
        Menu::delete(isset($info['first_menu'])?$info['first_menu']:$this->addon_name);
        return true;
    }

    /**
     * 插件启用方法
     */
    public function enable()
    {
        $info=get_addon_info($this->addon_name);
        Menu::enable(isset($info['first_menu'])?$info['first_menu']:$this->addon_name);
    }

    /**
     * 插件禁用方法
     */
    public function disable()
    {
        $info=get_addon_info($this->addon_name);
        Menu::disable(isset($info['first_menu'])?$info['first_menu']:$this->addon_name);
    }

    /**
     * 插件升级方法
     */
    public function upgrade()
    {
        $menu = include ADDON_PATH . $this->addon_name . DS . 'config' . DS . 'menu.php';
        Menu::upgrade($this->addon_name, $menu);
        $this->importData('updatedata');
    }
    /**
     * 任务添加成功猴子
     * @param $param
     */
    public function workteamTaskAdd($param){

        $param['key']='notice_new_task';
//        $param['admin_ids']=$param['admin_id'];//只通知任务负责人本人
		$param['admin_ids'] = $param['admin_id'].','.$param['admin_ids'];
        //消息通知
        Queue::push('addons\workteam\library\notice\queue\TaskJob',$param);
        //消息通知end
        return true;
    }

    /**
     * 导入升级的数据
     */
    protected function importData($sqlname="")
    {
        $sqlFile = ADDON_PATH . $this->addon_name . DS . 'config'. DS .$sqlname.'.sql';

        if (is_file($sqlFile)) {
            $lines = file($sqlFile);
            $templine = '';
            foreach ($lines as $line) {
                if (substr($line, 0, 2) == '--' || $line == '' || substr($line, 0, 2) == '/*') {
                    continue;
                }

                $templine .= $line;
                if (substr(trim($line), -1, 1) == ';') {
                    $templine = str_ireplace('__PREFIX__', config('database.prefix'), $templine);
                    $templine = str_ireplace('INSERT INTO ', 'INSERT IGNORE INTO ', $templine);
                    try {
                        Db::getPdo()->exec($templine);
                    } catch (\Exception $e) {
                        echo $e->getMessage();
                    }
                    $templine = '';
                }
            }
        }
    }
    /**
     * 脚本替换
     */
    public function viewFilter(& $content)
    {
        $database=config('database');
        $isexits=Db::query("SELECT 1 FROM  information_schema.COLUMNS WHERE  table_name='{$database['prefix']}admin' AND COLUMN_NAME='mobile' limit 1");
        if (!$isexits){
            Db::query("ALTER TABLE {$database['prefix']}admin ADD mobile VARCHAR(20)  NULL COMMENT '手机'");
        }
        $module = strtolower(request()->module());
        $controller=strtolower(request()->controller());
        if ($module == 'admin'&&$controller=='auth.admin') {
            $view=\think\View::instance();
            $mobile=isset($view->row->mobile)?$view->row->mobile:'';
            $str_rp='/<div class="form-group hidden layer-footer">/i';
            $new_rp='<div class="form-group"><label for="mobile" class="control-label col-xs-12 col-sm-2">手机:</label><div class="col-xs-12 col-sm-8"><input type="mobile" class="form-control" id="mobile" name="row[mobile]" value="'.$mobile.'"  /></div></div>';
            $content = preg_replace_callback($str_rp, function ($matches)  use ($new_rp){
                return $new_rp.'<div class="form-group hidden layer-footer">';
            }, $content);
        }
    }
}
